package com.atguigu.app;


import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

public class KafkaConsumerExample {
    /**

     //copilot begin
     * 读取kafka相关数据, kafka的版本为2.12-3.0.0，使用的是kafka-clients 3.0.0版本

     * @param args

     */

    public static void main(String[] args) {

        // 创建消费者对象

        Consumer<String, String> consumer = new KafkaConsumer<>(getProperties());

        // 订阅主题

        consumer.subscribe(Arrays.asList("topic_db"));

        // 消费消息

        while (true) {

            ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));

            for (ConsumerRecord<String, String> record : records)

                System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());

        }

    }

    private static Properties getProperties() {

        Properties props = new Properties();

        props.put("bootstrap.servers", "test60:9092");

        props.put("group.id", "test");

        props.put("enable.auto.commit", "true");

        props.put("auto.commit.interval.ms", "1000");

        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

        return props;

    }
     //copilot end



}